package org.one.afternoon.controller;

import org.one.afternoon.common.Result;
import org.one.afternoon.domain.Activity;
import org.one.afternoon.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.one.afternoon.common.Result.success;


@RestController
@RequestMapping(value = "/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @CrossOrigin 
    @GetMapping("/all")
    public Result<List<Activity>> findAll() {
        List<Activity> all = activityService.findAll();
        return success(all);
    }


    @CrossOrigin
    @PostMapping("/queryOne")
    @ResponseBody
    public Result<List<Activity>> queryOne(@RequestBody Activity activity) {
        List<Activity> activities = activityService.queryOne(activity);
        return success(activities);
    }

    @CrossOrigin
    @PostMapping("/insertOne")
    @ResponseBody
    public Result<String> addOne(@RequestBody Activity activity) {
        boolean insert = activityService.insert(activity);
        if (insert) {
            return Result.success("添加数据成功");
        }
        return Result.error("添加数据失败");
    }

    @CrossOrigin
    @PostMapping("/deleteOne")
    @ResponseBody
    public Result<String> deleteOne(@RequestParam Integer index) {
        boolean result = activityService.deleteById(index);
        if(result){
            return success("删除成功");
        }
        return Result.error("删除失败");
    }

    @CrossOrigin
    @PostMapping("/updateOne")
    @ResponseBody
    public Result<String> updateOne(@RequestBody Activity activity) {
        boolean update = activityService.update(activity);
        if(update){
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }
}
